<template>
	<view>
		<view>
			<up-grid :border="false" @click="click">
				<up-grid-item v-for="(baseListItem,baseListIndex) in baseList" :name="baseListItem.name" :key="baseListIndex">
					{{baseListItem.name}}
					<up-icon :customStyle="{paddingTop:20+'rpx'}" :name="baseListItem.name" :size="22"></up-icon>
					<text class="grid-text">{{baseListItem.title}}</text>
				</up-grid-item>
			</up-grid>
			<up-toast ref="uToastRef" />
		</view>

		这里是id为{{dataId}}的新闻的详情页
		<image :src="houseDetail.image"></image>
		<view>{{houseDetail.title}}</view>
		<view>户型：{{houseDetail.type}}</view>
		<view>面积：{{houseDetail.areaNum}}</view>
		<view>单价：{{houseDetail.unitPrice}}</view>
		<up-button @click="doPay">考试支付</up-button>
		房源经纪人:id是{{houseDetail.personId}}
		<up-button @click="goChat">资讯聊天</up-button>
	</view>
</template>

<script setup>
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		debounce
	} from 'uview-plus';
	import {
		ref
	} from 'vue';
	const baseList = ref([  
	    {  
	        name: 'photo',  
	        title: '图片'  
	    },  
	    {  
	        name: 'lock',  
	        title: '锁头'  
	    },  
	    {  
	        name: 'list',  
	        title: '房源列表'  
	    },  
	]); 
	const click = (name) => {
		console.log(name)
		if(name==='list') {
			uni.navigateTo({
				url: '/pages/list/list'
			})
		} else if(name==='lock') {
			uni.navigateTo({
				url: '/pages/my/my'
			})
		}
	}
	const houseObj = uniCloud.importObject('house')
	const dataId = ref('')
	// 模拟的我的的ID和房源经济人的id
	const meId = ref('bin')
	const houseDetail = ref({})

	onLoad((options) => {
		console.log(options.id)
		dataId.value = options.id
		getDetail()
		console.log(options.title)
	})
	const getDetail = async () => {
		// 云对象
		// const detail = await houseObj.getDetail(dataId.value)
		// if (detail.data.length > 0) {
		// 	houseDetail.value = detail.data[0]
		// }
		// 云函数
		uniCloud.callFunction({
			name: 'houseDetail',
			data: {
				id: dataId.value
			},
			success(res) {
				console.log(res)
				if(res.result.data.length>0) {
					houseDetail.value = res.result.data[0]
				}
			}
		})
	}
	const goChat = () => {
		uni.navigateTo({
			url: '/pages/linechat/linechat?meId=' + meId.value + '&toId=' + houseDetail.value.personId
		})
	}
	const doPay = () => debounce(() => {
		console.log('触发支付方法')
		// 判断登录状态
		// 已知：登录之后本地会存储token，如果现在本地没有token
		const token = uni.getStorageSync('token')
		if (!token) {
			// 没有登陆过
			uni.navigateTo({
				url: '/pages/login/login'
			})
		}
		uni.requestPayment({
			provider: 'wxpay',
			timeStamp: String(Date.now()),
			nonceStr: 'A1B2C3D4E5',
			package: 'prepay_id=wx20180101abcdefg',
			signType: 'MD5',
			paySign: '',
			success: function(res) {
				console.log('success:' + JSON.stringify(res));
			},
			fail: function(err) {
				console.log('fail:' + JSON.stringify(err));
			}
		});
	}, 500)
</script>

<style>

</style>